import { defineStore } from 'pinia'

// 定义接口的根路径
const backedHost = 'http://weapi.uodrad.top'

// 存储全局变量的数据仓
export const useStore = defineStore('index', {
  state: () => {
    return {
      backedHost: backedHost,
      userInfo: null,
    }
  },
  actions: {
    setUserInfo() {
      const xhr = new XMLHttpRequest();
      xhr.open('GET', backedHost + '/user/now_user_info', false); // 设置 async 为 false
      xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem('token'));
      xhr.send();

      if (xhr.status === 200) {
        const response = JSON.parse(xhr.responseText);
        if (response.code === 2000) {
          // 设置用户信息
          this.userInfo = response.data
        }
      } else {
        if (JSON.parse(xhr.responseText).code !== 4030) {
          location.reload()
        }       
      }
    }    
  }
})
